package com.wc.算法提高课.C第三章_图论.单源最短路的综合应用.道路与航线;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/10/11 12:27
 * @description https://www.acwing.com/problem/content/344/
 */
public class Main {
    /**
     * 思路：拓扑排序 + 并查集 + dijkstra
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 25010, M = 150100, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] p = new int[N], dist = new int[N], id = new int[N];
    static Queue<Integer> q = new LinkedList<>();
    static int[] din = new int[N];
    static ArrayList<ArrayList<Integer>> blocks = new ArrayList<>();
    static boolean[] st = new boolean[N];
    static int n, mr, mp, S, idx = 1, bcnt = 0;

    public static void main(String[] args) {
        n = sc.nextInt();
        mr = sc.nextInt();
        mp = sc.nextInt();
        S = sc.nextInt();
        for (int i = 0; i <= n; i++) {
            blocks.add(new ArrayList<>());
            p[i] = i;
        }
        while (mr-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
            a = find(a);
            b = find(b);
            if (a != b) {
                p[a] = b;
            }
        }
        // 分装block
        for (int i = 1; i <= n; i++) {
            int x = find(i);
            if (id[x] == 0) id[x] = ++bcnt;
            id[i] = id[x];
            blocks.get(id[x]).add(i);
        }
        while (mp-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            din[id[b]]++;
        }
        topsort();
        for(int i = 1; i <= n; i++){
            if (dist[i] > INF / 2) out.println("NO PATH");
            else out.println(dist[i]);
        }
        out.flush();
    }

    static void topsort() {
        Arrays.fill(dist, 1, n + 1, INF);
        dist[S] = 0;
        for (int i = 1; i <= bcnt; i++) {
            if (din[i] == 0) q.add(i);
        }
        while (!q.isEmpty()) {
            int bid = q.poll();
            dijkstra(bid);
        }
    }

    static void dijkstra(int bid) {
        PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(o -> o[0]));
        for (int i : blocks.get(bid)) {
            heap.add(new int[]{dist[i], i});
        }
        while (!heap.isEmpty()) {
            int t = heap.poll()[1];
            if (st[t]) continue;
            st[t] = true;
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (dist[j] > dist[t] + w[i]) {
                    dist[j] = dist[t] + w[i];
                    if (id[j] == id[t]) heap.add(new int[]{dist[j], j});
                }
                if (id[j] != id[t] && --din[id[j]] == 0) q.add(id[j]);
            }
        }
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    static int find(int x) {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
